# -*- encoding: utf-8 -*-
"""
PyCharm tools
2022年09月10日
by littlefean
"""
from typing import *


def strChange(string: str) -> str:
    for char in "abcdefghijklmnopqrstuvwxyz".upper():
        string = string.replace(char, f"-{char.lower()}")
    return string


def valueToStr(v) -> str:
    if v == 0:
        return "0"
    if isinstance(v, int):
        return f"{v}px"
    if isinstance(v, float):
        return f"{v * 100}%" if v != 0 else "0"
    if isinstance(v, tuple):
        if len(v) == 3:
            return f"rgb{repr(v)}"
        elif len(v) == 4:
            return f"rgba{repr(v)}"
    if isinstance(v, str):
        return v
    return ""


def rateToStr(n):
    return "0" if n == 0 else f"{n * 100}%"


def cssPairToStr(cssName: str, cssValue: Any) -> str:
    """
    一个将css键值对转化成 css内部一条的字符串
    :param cssName:
    :param cssValue:
    :return:
    """
    if type(cssValue) not in (int, float, str, tuple):
        return ""

    cssName = strChange(cssName)

    if cssName.startswith("animation") and type(cssValue) in [int, float]:
        return f"{cssName}: {cssValue}s;"
    if cssName in ["width", "height"]:
        return f"{cssName}: {cssValue}px;"
    if cssName in ["border-radius"]:
        return f"{cssName}: {rateToStr(cssValue)};"
    # 是颜色
    if "color" in cssName and isinstance(cssValue, tuple):
        if len(cssValue) == 3:
            return f"{cssName}: rgb{repr(cssValue)};"
        elif len(cssValue) == 4:
            return f"{cssName}: rgba{repr(cssValue)};"
    if "margin" in cssName:
        return f"{cssName}: {cssValue}px;"
    if cssName in ["left", "top"]:
        return f"{cssName}: {rateToStr(cssValue)};"
    return f"{cssName}: {cssValue};"


def main():
    return None


if __name__ == "__main__":
    main()
